<template>
  <div>
    <div class="herdertop">
      <img src="../../assets/top.png" alt="" class="topImg" />
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>服务商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>套餐列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="center" style="padding-top: 0px;">
      <div class="hander">套餐列表</div>
      <div class="cold">
        <!-- 套餐状态 -->
        <div class="isPutAway marginRight">
          <span>套餐状态：</span>
          <el-select
            v-model="isPutAway"
            placeholder="请选择"
            @change="changeState"
            clearable
          >
            <el-option
              v-for="item in isPutAwayOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>

        <div class="userSearch">
          <el-select v-model="userSearch" placeholder="请选择">
            <el-option
              v-for="item in staffSearch"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-input
            v-model="goodsPackageName"
            placeholder="请输入套餐名称"
          ></el-input>
          <!-- <el-button
            icon="el-icon-search"
            @click="searchForUserInformation"
          ></el-button> -->
        </div>
        <el-button
          style="background-color:#fd804c;color: #fff;margin-left:20px;"
          @click.native.prevent="searchForUserInformation"
          >查询</el-button
        >
        <el-button type="warning" @click.native.prevent="isEmpty"
          >清空</el-button
        >
      </div>
      <!-- 表格 -->
      <div class="implement">
        <div class="btn">
          <div class="left">
            <el-button @click="upAndDown(1)">批量上架</el-button>
            <el-button @click="upAndDown(0)">批量下架</el-button>
          </div>
          <div class="right">
            <el-button @click="modif">新增套餐</el-button>
            <!-- <el-button :disabled="multipleSelection.length == 0 ? true : false">
              <download-excel
                :data="multipleSelection"
                :fields="json_fields"
                name="套餐表.xls"
              >
                导出</download-excel
              >
            </el-button> -->
          </div>
        </div>
        <el-table
          :data="tableData"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"> </el-table-column>
          <el-table-column prop="goodsPackageName" label="套餐名称" width="120">
            <template slot-scope="scope">
              <div>{{ scope.row.goodsPackageName }}</div>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="businessAreaName" label="套餐优惠" width="120">
            <template slot-scope="scope">
              <div>{{ scope.row.businessArea }}</div>
            </template>
          </el-table-column> -->
          <el-table-column
            prop="includeGoodsClassify"
            label="包含商品分类"
            width="120"
          >
            <template slot-scope="scope">
              <div>{{ scope.row.includeGoodsClassify }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="includeAreasName" label="包含区域" width="120">
            <template slot-scope="scope">
              <div>{{ scope.row.includeAreasName }}</div>
            </template>
          </el-table-column>
          <el-table-column
            prop="updateTime"
            width="180"
            label="最近一次修改时间"
          >
            <template slot-scope="scope">
              <div>{{ scope.row.updateTime }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="isPutAway" label="套餐状态" width="120">
            <template slot-scope="scope">
              <div>
                <span>{{ scope.row.isPutAway == 1 ? "上架" : "下架" }}</span>
                <el-switch
                  :value="scope.row.isPutAway == 1"
                  active-color="#ff811e"
                  inactive-color="#f2f2f6"
                  @change="value => changeSwitchStatus(value, scope.row)"
                >
                </el-switch>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="updater" label="操作人" width="120">
            <template slot-scope="scope">
              <div>{{ scope.row.updater }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                @click="handleClickEdit(scope.row)"
                style="color: #fc9e76;"
                type="text"
                size="small"
                >修改</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <div></div>
        <div>
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-size="pageSize"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      payMd: "",
      goodsPackageName: "",
      goodsPackageName: "",
      isPutAway: "",
      isPutAwayOptions: [
        { value: 0, label: "下架" },
        { value: 1, label: "上架" }
      ],
      multipleSelection: [],

      payMdsOptions: [],
      userSearch: 0,
      pageIndex: 1,
      pageSize: 20,
      total: 0,
      json_fields: {
        //导出Excel表格的表头设置
        套餐名称: "goodsPackageName",
        套餐优惠: "businessAreaName",
        包含区域: "includeAreasName",
        最近一次修改时间: "updateTime",
        套餐状态: "isPutAway",
        操作人: "updater"
      },

      staffSearch: [{ value: 0, label: "套餐名称" }]
    };
  },
  activated() {
    this.DetailByPage();
  },
  methods: {
    // 更换状态数据
    changeState(val) {
      // console.log("更换状态数据,,,,", val);
      // this.isPutAway = val;
      // this.pageIndex = 1;
      // this.DetailByPage();
    },
    // 精准模糊查找
    searchForUserInformation() {
      if (this.userSearch == 0) {
        this.goodsPackageName = this.goodsPackageName || "";
      }
      this.pageIndex = 1;
      this.DetailByPage();
    },
    handleSelectionChange(val) {
      console.log("handleSelectionChange,,,,,,,", val);
      this.multipleSelection = val;
    },

    DetailByPage() {
      this.$http({
        url: "/hssgoodsadmin/serviceGoodsPkg/getGoodsPkgByPage",
        method: "POST",
        data: {
          goodsPackageName: this.goodsPackageName,
          isPutAway: this.isPutAway,
          page: this.pageIndex,
          size: this.pageSize
        }
      }).then(res => {
        this.tableData = res.data.list;
        this.total = parseInt(res.data.total);
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.DetailByPage();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.DetailByPage();
    },
    // 跳转新增套餐
    modif() {
      this.$router.push({
        path: "/modify"
      });
    },
    // 跳转套餐编辑页
    handleClickEdit(row) {
      this.$router.push({
        path: "/editPackage",
        query: {
          goodsPackageCode: row.goodsPackageCode
        }
      });
    },
    async upAndDown(isPutAway) {
      // 批量上架下架
      if (this.multipleSelection.length === 0) {
        this.$message.error("请先勾选套餐");
        return false;
      }
      const goodsPackageCodes = [];
      this.multipleSelection.forEach(item => {
        goodsPackageCodes.push(item.goodsPackageCode);
      });
      const data = {
        isPutAway: isPutAway,
        goodsPackageCodes
      };
      await this.$http({
        url: "/hssgoodsadmin/serviceGoodsPkg/batchPutAway",
        method: "post",
        data
      })
        .then(() => {
          if (isPutAway == 0) {
            this.$message.success("批量下架成功");
            this.DetailByPage();
          } else if (isPutAway == 1) {
            this.$message.success("批量上架成功");
            this.DetailByPage();
          }
        })
        .catch(err => {
          if (isPutAway == 0) {
            this.$message.error("批量下架失败");
          } else if (isPutAway == 1) {
            this.$message.error("批量上架失败");
          }
        });
    },
    async changeSwitchStatus(value, row) {
      // 单独上下架
      const goodsPackageCodes = [];
      goodsPackageCodes.push(row.goodsPackageCode);
      const data = {
        goodsPackageCodes,
        isPutAway: value === true ? 1 : 0
      };
      await this.$http({
        url: "/hssgoodsadmin/serviceGoodsPkg/batchPutAway",
        method: "post",
        data
      })
        .then(() => {
          this.$message.success("修改成功");
          this.DetailByPage();
        })
        .catch(err => {
          this.$message.error("修改失败");
        });
    },
    isEmpty() {
      // 搜索清空
      this.isPutAway = "";
      this.goodsPackageName = "";
    }
  },
  beforeRouteEnter(to, from, next) {
    // 套餐跳转过来页面刷新
    console.log(from);
    if (from.name === "editPackage" || from.name === "modify") {
      next();
      // this.$router.go(0)
      window.location.reload();
    } else {
      next();
    }
  }
};
</script>
<style scoped>
marginRight {
  display: flex;
  align-items: center;
  width: 33%;
}
.herdertop {
  height: 8vh;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-sizing: border-box;
}
.topImg {
  margin-right: 20px;
}
.userSearch {
  display: flex;
  align-items: center;
  margin-left: 50px;
  width: 33%;
}
.cold {
  display: flex;
}
.implement {
  background-color: #f5f5f5;
  padding: 20px;
  margin-top: 80px;
  margin-top: 30px;
  border-radius: 6px;
}
.btn {
  display: flex;
  margin-bottom: 10px;
}
.left {
  display: flex;
  flex: 1;
}
.right {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: flex-end;
}
.pagination {
  margin-top: 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}
</style>
